package by.semerikov.action;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import by.semerikov.constants.Results;
import by.semerikov.data.SessionHandler;
import by.semerikov.exceptions.ConnectorException;
import by.semerikov.exceptions.DAOException;
import by.semerikov.model.Person;

public class LogoutAction extends Action{
	private static final Logger log = Logger.getLogger(LogoutAction.class);
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			 {
		try{
			
			SessionHandler sh = new SessionHandler(request);
			Person user = sh.getUser();
			log.info("User" + user.getName() +  "end Work");
			sh.DeleteSession();
			return mapping.findForward(Results.SUCCESS);
		}
		catch (DAOException e) {
			log.error(e);
			ActionMessages errors = new ActionMessages();
			errors.add("errorMessage", new ActionMessage(e.getMessage()));
			saveErrors(request, errors);
			//e.printStackTrace();
			return mapping.findForward(Results.ERROR);
		} catch (ConnectorException e) {
			log.error(e);
			ActionMessages errors = new ActionMessages();
			errors.add("errorMessage", new ActionMessage(e.getMessage()));
			saveErrors(request, errors);
			//e.printStackTrace();
			return mapping.findForward(Results.ERROR);
		}
		catch (Exception e) {
			log.error(e);
			ActionMessages errors = new ActionMessages();
			errors.add("errorMessage", new ActionMessage(e.getMessage()));
			saveErrors(request, errors);
			//e.printStackTrace();
			return mapping.findForward(Results.ERROR);
		}
		
	}
}
